package com.medium.house.controller;

import com.medium.house.entity.WechatUser;
import com.medium.house.enums.HouseTypeEnum;
import com.medium.house.request.WechatAuthUserRequestDto;
import com.medium.house.service.WechatUserService;
import com.medium.house.vo.ResultVO;
import com.medium.house.vo.ResultVOUtil;
import com.medium.house.request.WechatUserRequestDto;
import com.medium.house.vo.WechatSignVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 主要用途: 用户信息管理
 *
 * @author yizhiwazi
 * @see Api 描述类/接口的主要用途
 * @see ApiOperation 描述方法用途
 * @see ApiImplicitParams 描述方法的参数(Multi-Params)
 * @see ApiImplicitParam  描述方法的参数
 * @see ApiIgnore 忽略某类/方法/参数的文档
 */
@Api(value = "/user",description="openid获取及设置",tags={"用户"})
@RestController
@RequestMapping("/api/user/")
@Slf4j
@SuppressWarnings("all")
public class WechatUserController {
    @Autowired
    private WechatUserService wechatUserService;


    /**
     * 获取用户信息
     * @param openid
     * @return
     */
    @ApiOperation("根据openid获取用户信息")
    @PostMapping("getWechatUserByOpenid")
    public ResultVO getWechatUser(@RequestParam String openid){
        if (StringUtils.isEmpty(openid)){
            return ResultVOUtil.error("参数异常");
        }
        WechatUser wechatUser = wechatUserService.getWechatUserByOpenid(openid);
        if (null == wechatUser){
            return ResultVOUtil.error("用户信息不存在");
        }
        WechatUserRequestDto wechatUserVO = new WechatUserRequestDto();
        BeanUtils.copyProperties(wechatUser,wechatUserVO);
        return  ResultVOUtil.success(wechatUserVO);
    }

    /**
     * 绑定手机 / 姓名
     * @return
     */
    @ApiOperation("会员认证,绑定手机、姓名")
    @PostMapping("setUserInfo")
    public ResultVO bind(@Valid WechatAuthUserRequestDto wechatAuthUserRequestDto, BindingResult bindingResult){
        log.info("WechatUserController setUserInfo requestDto:{}",wechatAuthUserRequestDto);
        //参数校验
        if(bindingResult.hasErrors()){
            return ResultVOUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        WechatUser wechatUser = wechatUserService.getWechatUserByOpenid(wechatAuthUserRequestDto.getOpenid());
        if (null == wechatUser){
            return ResultVOUtil.error("用户信息不存在");
        }
        BeanUtils.copyProperties(wechatAuthUserRequestDto,wechatUser);
        if (wechatUserService.update(wechatUser) == 0 ){
            return ResultVOUtil.error("连接超时，请稍后再试...");
        }
        return  ResultVOUtil.success();
    }

/*    *//**
     * 绑定手机 / 姓名
     * @return
     *//*
    @ApiOperation("绑定手机/姓名")
    @PostMapping("myRelease")
    public ResultVO myRelease(@RequestParam String openid,@RequestParam Integer type){
        if (StringUtils.isEmpty(openid)){
            return ResultVOUtil.error("参数异常");
        }
        return  ResultVOUtil.success();
    }*/
}
